<template>
  <div class="edu-page-warp">
    <!--列表-->
    <div class="edu-page-title mg-top">{{ $t('message.filterCondition') }}</div>
    <el-card shadow="never" body-style="padding: 24px;">
      <el-form :inline="true" :model="queryForm">
        <el-row :gutter="20" style="height: 50px;">
          <el-col :span="6">
            <el-form-item label="异动大类名称">
              <el-input
                v-model="queryForm.keyWrod"
                placeholder="请输入"
                @keyup.enter.native="onSubmit"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="empty-box"></div>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button @click="reset">{{ $t('message.reset') }}</el-button>
              <el-button type="primary" @click="onSubmit">{{ $t('message.filter') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div class="edu-page-title mg-top">{{ $t('message.statusCategory') }}</div>
    <el-card shadow="never" body-style="padding: 24px;">
      <el-row class="border-bottom" style="height:56px;">
        <el-col :span="20">
          <div class="empty-box"/>
        </el-col>
        <el-col :span="4">
          <div class="float-right">
            <el-button type="primary" @click="showAddCategoryDialog()" size="mini">新增</el-button>
          </div>
        </el-col>
      </el-row>
      <el-table :data="statusCategories" style="width: 100%">
        <el-table-column type="index" label="编号" :index="indexMethod"></el-table-column>
        <el-table-column label="异动大类">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="英文名称">
          <template slot-scope="scope">{{ scope.row.nameEn }}</template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button @click="editCategory(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="deleteCategory(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="edu-pagination">
        <div class="float-right">
          <el-pagination
            :current-page="queryForm.pageNum_"
            :page-sizes="[20,50,100,500,1000]"
            :page-size="queryForm.pageSize_"
            :total="queryForm.total"
            layout="total, sizes, prev, pager, next, jumper"
            style="float: right;margin:10px 60px 0 0;"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <!--添加弹窗-->
    <el-dialog
      :title.sync="dialogConfig.dialogTitle"
      :visible.sync="dialogConfig.showDialog"
      width="40%"
      :before-close="handleDialogClose"
    >
      <el-form
        :model="dialogConfig.formData"
        :inline="true"
        :rules="dialogConfig.rules"
        ref="addItemForm"
      >
        <el-form-item label="异动大类名称" :label-width="dialogConfig.formLabelWidth" prop="name">
          <el-input v-model="dialogConfig.formData.name"></el-input>
        </el-form-item>
        <el-form-item label="英文名称" :label-width="dialogConfig.formLabelWidth" prop="nameEn">
          <el-input v-model="dialogConfig.formData.nameEn"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogConfig.showDialog = false">取 消</el-button>
        <el-button type="primary" @click="handlerAddCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as API from 'common/src/api/studentStatus'

export default {
    data() {
        function checkIsExistCategoryName(rule, value, callback) {
            if (!value) {
                return callback(new Error('输入不能为空'))
            }
            API.isExistCategory({ name: value }).then((response) => {
                let { code, data } = response
                if (code === 200 && data) {
                    callback(new Error('名称已经存在'))
                } else {
                    callback()
                }
            })
        }
        return {
            statusCategories: [],
            queryForm: {
                pageSize_: 20,
                pageNum_: 1,
                keyWrod: '',
            },
            dialogConfig: {
                dialogTitle: '添加异动大类',
                showDialog: false,
                flagEdit: false,
                formData: {
                    name: '',
                    nameEn: '',
                },
                formLabelWidth: '100px',
                rules: {
                    name: [
                        {
                            required: true,
                            message: '输入不能为空',
                            trigger: 'blur',
                        },
                        {
                            validator: checkIsExistCategoryName,
                            trigger: 'blur',
                        },
                    ],
                },
            },
        }
    },
    methods: {
        editCategory(row) {
            let { id, name, nameEn } = row
            this.initForm({ id, name, nameEn })
            this.dialogConfig.flagEdit = true
            this.dialogConfig.showDialog = true
        },
        deleteCategory(row) {
            API.deleteStatusCategory(row).then((response) => {
                if (response.code === 200) {
                    this.$message({
                        message: '删除成功',
                        type: 'success',
                    })
                    this.onSubmit();
                }
            })
        },
        onSubmit() {
            API.getStatusCategories(this.queryForm).then((response) => {
                let { code, data } = response
                if (code === 200) {
                    this.statusCategories = data.list
                }
            })
        },
        showAddCategoryDialog() {
            this.initForm({
                name: '',
                nameEn: '',
            })
            this.dialogConfig.flagEdit = false
            this.dialogConfig.showDialog = true
        },
        handlerAddCategory() {
            this.$refs['addItemForm'].validate((valid) => {
                if (valid) {
                    if (this.dialogConfig.flagEdit) {
                        //走修改的接口
                        API.editStatusCategory(this.dialogConfig.formData)
                            .then((data) => {
                                if (data.code === 200) {
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success',
                                    })
                                    this.onSubmit()
                                }
                            })
                            .finally(() => {
                                this.dialogConfig.showDialog = false
                            })
                    } else {
                        // 走添加的接口
                        API.addStatusCategory(this.dialogConfig.formData)
                            .then(
                                (data) => {
                                    if (data.code === 200) {
                                        this.$message({
                                            message: '新增成功',
                                            type: 'success',
                                        })
                                        this.onSubmit()
                                    }
                                },
                                (xhr) => {}
                            )
                            .finally(() => {
                                this.dialogConfig.showDialog = false
                            })
                    }
                }
            })
        },
        isExistCategory() {
            API.isExistCategory().then((response) => {
                let { code, data } = response
            })
        },
        handleSizeChange() {},
        handleCurrentChange() {},
        initForm(obj) {
            this.dialogConfig.formData = obj
        },
        indexMethod(index) {
            return index + 1
        },
    },
    mounted() {
        this.onSubmit()
    },
    handleDialogClose() {},
}
</script>
<style>
</style>

